#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "Util.hpp"
const std::string sep = "\r\n";
const std::string default_root = "./wwwroot";
const std::string home_page = "index.html"; // 默认首页
const std::string html_404 = "wwwroot/404.html";
// 一个用户看到的网页结果,可能是多个资源组合而成的,所以要获取一张完整的网页效果,我们的浏览器一定会发起多次http请求
class HttpRequest
{
public:
    HttpRequest()
    {
    }
    ~HttpRequest()
    {
    }
    void parse()
    {
        std::string line = Util::getOneLine(inbuffer, sep);
        if (line.empty())
            return;
        std::cout << "line : " << line << std::endl;
        std::stringstream ss(line);
        ss >> method >> url >> httpversion;
        // 添加web默认路径
        path = default_root; //./wwwroot
        path += url;         //./wwwroot/.......
        if (path[path.size() - 1] == '/')
            path += home_page;
        // 获取path对应的资源后缀
        auto pos = path.rfind(".");
        if (pos == std::string::npos)
        {
            suffix = ".html";
        }
        else
        {
            suffix = path.substr(pos);
        }
        // 得到资源的大小
        struct stat st;
        int n = stat(path.c_str(), &st);
        if (n == 0)
            size = st.st_size;
        else
            size = -1;
    }

public:
    std::string inbuffer;
    // std::string reqline;
    // std::vector<std::string> reqheader;
    // std::string body;
    std::string method;
    std::string url;
    std::string httpversion;
    std::string path;
    std::string suffix;
    std::string parm;
    int size;
};
class HttpResponse
{
public:
    std::string outbuffer;
};